package com.swh.user.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.core.codec.Encoder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.swh.utils.IdUtil;
import com.swh.user.dao.TbAdminDao;
import com.swh.user.entity.TbAdmin;

/**
 * @author swh
 * @Description 管理员 服务层
 * @date 2021-01-15 17:27:10
 */
@Service
public class TbAdminService {

    @Autowired
    private TbAdminDao tbAdminDao;

    @Autowired
    private IdUtil idUtil;

    @Autowired
    BCryptPasswordEncoder encoder;


    /**
     * 管理员登录
     *
     * @return
     **/
    public TbAdmin login(String loginname, String password) {
        TbAdmin admin = tbAdminDao.findByLoginname(loginname);
        if (admin != null && encoder.matches(password, admin.getPassword())) {
            return admin;
        }
        return null;
    }

    /**
     * 增加用户
     *
     * @param tbAdmin
     */
    public void add(TbAdmin tbAdmin) {
        tbAdmin.setId(idUtil.nextId() + "");

        //使用BCrypt加密
        tbAdmin.setPassword(encoder.encode(tbAdmin.getPassword()));

        tbAdminDao.save(tbAdmin);
    }


    /**
     * 查询全部列表
     *
     * @return
     */
    public List<TbAdmin> findAll() {
        return tbAdminDao.findAll();
    }

    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<TbAdmin> findSearch(Map whereMap, int page, int size) {
        Specification<TbAdmin> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return tbAdminDao.findAll(specification, pageRequest);
    }

    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<TbAdmin> findSearch(Map whereMap) {
        Specification<TbAdmin> specification = createSpecification(whereMap);
        return tbAdminDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public TbAdmin findById(String id) {
        return tbAdminDao.findById(id).get();
    }


    /**
     * 修改
     *
     * @param tbAdmin
     */
    public void update(TbAdmin tbAdmin) {
        tbAdminDao.save(tbAdmin);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        tbAdminDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<TbAdmin> createSpecification(Map searchMap) {

        return new Specification<TbAdmin>() {

            @Override
            public Predicate toPredicate(Root<TbAdmin> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //登陆名称
                if (searchMap.get("loginname") != null && !"".equals(searchMap.get("loginname"))) {
                    predicateList.add(cb.like(root.get("loginname").as(String.class), "%" + (String) searchMap.get("loginname") + "%"));
                }
                //密码
                if (searchMap.get("password") != null && !"".equals(searchMap.get("password"))) {
                    predicateList.add(cb.like(root.get("password").as(String.class), "%" + (String) searchMap.get("password") + "%"));
                }
                //状态
                if (searchMap.get("state") != null && !"".equals(searchMap.get("state"))) {
                    predicateList.add(cb.like(root.get("state").as(String.class), "%" + (String) searchMap.get("state") + "%"));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

}
